//
//  RecommendationTableViewCell.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/7/30.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit

class RecommendationTableViewCell: UITableViewCell {
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    var backSeleItemID:BackIntValue!
    
    var needLoginClosure:TouchBlock!
    
    var dataArray:[SiteGoodsModel] = [SiteGoodsModel](){
        didSet{
            self.collectionView.reloadData()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize.init(width: 140, height: 185)
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 5
        layout.sectionInset = UIEdgeInsetsMake(5, 7, 5, 7)
        self.collectionView.setCollectionViewLayout(layout, animated: true)
        self.collectionView.register(UINib.init(nibName: "ExplosiveGoodsCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ExplosiveGoodsCollectionViewCell")
        self.collectionView.showsHorizontalScrollIndicator = false
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension RecommendationTableViewCell : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
         return self.dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ExplosiveGoodsCollectionViewCell", for: indexPath) as! ExplosiveGoodsCollectionViewCell
        cell.reload(withModel: self.dataArray[indexPath.row])
        
        cell.loginClosure = {
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "addCartNeedLogin"), object: nil)
        }
        
        cell.backGoodsId = { (goodsID) in
            
            Utils.insertCart(goodsId: goodsID) { (value) in
                if value.0 {
                    PKhud.showText(text: "添加成功~")
                }else{
                    PKhud.showText(text: value.1)
                }
            }
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if self.backSeleItemID != nil {
            self.backSeleItemID(self.dataArray[indexPath.row].id)
        }
    }
}



















